package com.xckj.zuoliao.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.zuoliao.R;
import com.xckj.zuoliao.bean.DetailsBean;
import com.xckj.zuoliao.global.Constant;
import com.xckj.zuoliao.utils.SpUtil;

import java.util.List;

/**
 * @Author: 代码的搬运工
 * @Describe: 期数
 * @Date: 2024/4/1 12:42
 */
public class NumberOfPeriodsAdapter extends RecyclerView.Adapter {
    private List<DetailsBean.GameStageLists> gameStageLists;
    private OnItemClickListener onItemClickListener;
    private Context context;

    public NumberOfPeriodsAdapter(Context context) {
        this.context = context;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<DetailsBean.GameStageLists> gameStageLists) {
        this.gameStageLists = gameStageLists;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_number_of_periods, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
        itemViewHolder.itemView.setOnClickListener(view -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(view, position);
            }
        });
        DetailsBean.GameStageLists gameStageList = gameStageLists.get(position);
        itemViewHolder.tvNumberOfPeriods.setText(gameStageList.stage_name);
        int selectedPosition = (int) SpUtil.get(context, Constant.SELECTED_POSITION, -1);
        if (selectedPosition == position) {
            itemViewHolder.tvNumberOfPeriods.setTextColor(context.getColor(R.color.number_of_periods_selected));
        } else {
            itemViewHolder.tvNumberOfPeriods.setTextColor(context.getColor(R.color.number_of_periods_unchecked));
        }
    }

    @Override
    public int getItemCount() {
        return gameStageLists == null ? 0 : gameStageLists.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        private final TextView tvNumberOfPeriods;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNumberOfPeriods = itemView.findViewById(R.id.tv_number_of_periods);
        }
    }

    public interface OnItemClickListener {
        /**
         * Item子View的点击事件
         *
         * @param view     当前视图
         * @param position 当前索引
         */
        void onItemClick(View view, int position);
    }
}
